package online.sh.cn.fy.common.api;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.sf.json.JSONObject;
import sun.util.logging.resources.logging;


public class WxApi {
	public static String getSystime(){
    	String time = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
    	return time;
    }
	
	//获取openid
		public String getOpenid(String code){
			try {
				String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxe643cc1b34aa8f25&secret=90ebfc583953b6cff690fc25eff11e91&code="+code+"&grant_type=authorization_code";
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=lines; 
		        } 
		        reader.close(); 
		        connection.disconnect(); 
		        return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}	
		
	//获取accesstoken
		public String getAccessToken(){
			try {
				String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxe643cc1b34aa8f25&secret=90ebfc583953b6cff690fc25eff11e91";
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=lines; 
		        } 
		        reader.close(); 
		        connection.disconnect(); 
		        return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}			
		
	//调用jssdk ticket
		public String getJsTicket(String accesstoken){
			try {
				String url="https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token="+accesstoken;
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=lines; 
		        } 
		        reader.close(); 
		        connection.disconnect(); 
		        return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}								
		
		
	//调用用户信息 通过普通接口
		public String getUserinfo(String accesstoken,String openid){
			try {
				String url="https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accesstoken+"&openid="+openid+"&lang=zh_CN";
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=lines; 
		        } 
		        reader.close(); 
		        connection.disconnect(); 
		        return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		
		
		//调用用户信息 通过网页授权接口
		public String getUserinfoByCode(String accesstoken, String openid) {
			try {
				String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accesstoken + "&openid=" + openid+ "&lang=zh_CN";
				URL getUrl = new URL(url);
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect();
				BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				String lines = null;
				String s = null;
				while ((lines = reader.readLine()) != null) {
					s = lines;
				}
				reader.close();
				connection.disconnect();
				return s;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		
		//调用用户手机信息
		public JSONObject getMoblieinfo(String mobile){
			try {
				String url="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+mobile;
				URL getUrl = new URL(url); 
				HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
				connection.connect(); 
		        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"gb2312")); 
		        String lines=null; 
		        String s=null;
		        while ((lines = reader.readLine()) != null) { 
		               s=s+lines; 
		        } 
		        reader.close(); 
		        connection.disconnect();
		        s=s.replace("null__GetZoneResult_ = ", "");
		        JSONObject json=JSONObject.fromObject(s);
		        return json;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		
		public static void main(String[] args) {
			System.out.println(new WxApi().getAccessToken());
		}
}
